package com.atzxh.exer;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @author 小红同学
 * @create 2023-04-08-17:42
 * @description
 */
public class StudentOS {
    private static List<Student> list = new ArrayList<>();
    private static Scanner sc = new Scanner(System.in);
    private static boolean flag = false;
    public static void main(String[] args) {
        init();
        while (true) {
            menu();
        }
    }

    private static void init() {
        Student s1 = new Student("1","张三","男",88);
        Student s2 = new Student("2","李四","男",81);
        Student s3 = new Student("3","王五","女",82);
        list.add(s1);
        list.add(s2);
        list.add(s3);
    }

    public static void menu() {

        System.out.println("********************");
        System.out.println("欢迎使用学生管理系统");
        System.out.println("0.退出菜单");
        System.out.println("1.增加学员信息");
        System.out.println("2.删除学员信息");
        System.out.println("3.查询全部学员");
        System.out.println("4.根据学号查询学员");
        System.out.println("5.修改学员学号");
        System.out.println("6.打印学员成绩");
        System.out.println("7.查询最高分学员");
        System.out.println("8.根据学员性别分类");
        System.out.println("9.根据成绩筛选学员");
        System.out.println("10.根据姓名查询学员");
        System.out.println("********************");
        System.out.println("请输入菜单序号：");

        int i = sc.nextInt();
        switch (i) {
            case 0:
                System.exit(0);
            case 1:
                addStu();
                break;
            case 2:
                delStuByID();
                break;
            case 3:
                getAllStu();
                break;
            case 4:
                getStuByID();
                break;
            case 5:
                editStuID();
                break;
            case 6:
                printlnScore();
                break;
            case 7:
                getMaxScoreStu();
                break;
            case 8:
                getStuBySex();
                break;
            case 9:
                getStuByScore();
                break;
            case 10:
                getStuByName();
                break;
            default:
                System.out.println("请输入正确序号！！！");
                break;
        }
    }

    // 6.打印学员成绩(从高到低)
    private static void printlnScore() {
        list.sort((o1, o2) -> {
            if ((o1.getScore() - o2.getScore()) > 0) {
                return -1;
            } else if ((o1.getScore() - o2.getScore()) < 0){
                return 1;
            } else {
                return 0;
            }
        });
        for (Student s : list) {
            System.out.println(s.getScore());
        }
    }

    // 3.查询全部学员
    private static void getAllStu() {
        for (Student s : list) {
            System.out.println(s.toString());
        }
    }

    // 4.根据学号查询学员
    private static void getStuByID() {
        System.out.println("请输入学员的学号：");
        String id = sc.next();
        for (Student s : list) {
            if (s.getId().equals(id)) {
                System.out.println(s.toString());
            }
        }
    }

    // 2.删除学员信息
    private static void delStuByID() {
        System.out.println("请输入学员的学号：");
        String id = sc.next();
        if (!idIsExist(id)) {
            // 此时学号不存在
            System.out.println("删除失败，学号不存在！！！");
            return;
        }
        int index = indexOfStu(id);
        list.remove(index);
        System.out.println("删除成功！！！");
    }

    // 1.增加学员信息
    private static void addStu() {
        System.out.println("请输入学号：");
        String ID = sc.next();
        // 判断学号是否存在
        while (idIsExist(ID)) {
            System.out.println("学号已存在，请重新输入学号：");
            ID = sc.next();
        }
        System.out.println("请输入姓名：");
        String name = sc.next();
        System.out.println("请输入性别：");
        String gender = sc.next();
        System.out.println("请输入成绩：");
        double score = sc.nextDouble();
        Student student = new Student(ID, name, gender, score);
        list.add(student);
        System.out.println("添加成功！！！");
    }

    // 10.根据姓名查询学员
    public static void getStuByName() {
        System.out.println("请输入姓名：");
        String name = sc.next();
        for (Student s : list) {
            if (s.getName().contains(name)) {
                flag = true;
                System.out.println(s.toString());
            }
        }
        if (flag == false) {
            System.out.println("查询失败，没有此学员！！！！");
        }
    }
    // 9.根据成绩筛选学员
    public static void getStuByScore() {
        System.out.println("请输入成绩：");
        double score = sc.nextDouble();
        for (Student s : list) {
            if (s.getScore() == score) {
                flag = true;
                System.out.println(s.toString());
                return;
            }
        }
        if (flag == false) {
            System.out.println("查询失败，没有该成绩的学员！！！");
        }
    }
    // 8.根据学员性别分类
    public static void getStuBySex() {
        System.out.println("请输入性别：");
        String gender = sc.next();
//        for (Student s : list) {
//            if (s.getGender().equals(gender)) {
//                System.out.println(s.toString());
//            }
//        }
        List<Student> studentList = list.stream()
                .filter(student -> student.getGender().equals(gender))
                .collect(Collectors.toList());
        for (Student s : studentList) {
            System.out.println(s.toString());
        }
    }
    // 5.修改学员学号
    public static void editStuID() {
        System.out.println("请输入学号：");
        String ID = sc.next();
        while (!idIsExist(ID)) {
            System.out.println("学号不存在,请重新输入！！！");
            System.out.println("请输入学号：");
            ID = sc.next();
        }
        for (Student s : list) {
            if (s.getId().equals(ID)) {
                System.out.println("请输入修改后的学号：");
                String id = sc.next();
                Student student = new Student(id,s.getName(),s.getGender(),s.getScore());
                list.remove(s);
                list.add(student);
                System.out.println("修改成功！！！");
            }
        }
    }
    // 7.查询最高分学员
    public static void getMaxScoreStu() {
        Student student = list.get(0);
        for (int i = 1; i < list.size(); i++) {
            Student s = list.get(i);
            if (s.getScore() > student.getScore()) {
                student = s;
            }
        }
        System.out.println(student.toString());
    }

    // 判断id是否存在
    private static boolean idIsExist(String id) {
        for (Student s : list) {
            if (s.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    // 获得该id的学生在list中的index
    private static int indexOfStu(String id) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student.getId().equals(id)) {
                index = i;
            }
        }
        return index;
    }
}


class Student {
    private String id;
    private String name;
    private String gender;
    private double score;

    public Student() {
    }

    public Student(String id, String name, String gender, double score) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.score = score;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", score=" + score +
                '}';
    }


}
